//package com.plian.system.service.zzd.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.plian.Tools.CollectionUtil;
//import com.plian.Tools.DateUtil;
//import com.plian.Tools.StringUtil;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.dao.bt.baseinfo.BaseInfoDao;
//import com.plian.system.dto.zzd.ZZDQuery;
//import com.plian.system.entity.bi.CompanyStokeright;
//import com.plian.system.entity.bi.MixedAnnualMonth;
//import com.plian.system.entity.bi.MixedFinishMonth;
//import com.plian.system.entity.fr.bulletin.BulletinValue;
//import com.plian.system.entity.fr.bulletin.NewBulletin;
//import com.plian.system.entity.pm.property.PropertyBasics;
//import com.plian.system.entity.qg.subsidiary.SubsidiaryMixedReform;
//import com.plian.system.entity.sys.Org;
//import com.plian.system.mapper.bi.CompanyStokerightMapper;
//import com.plian.system.mapper.bi.MixedAnnualMonthMapper;
//import com.plian.system.mapper.bi.MixedFinishMonthMapper;
//import com.plian.system.service.fr.bulletin.IBulletinValueService;
//import com.plian.system.service.fr.bulletin.INewBulletinService;
//import com.plian.system.service.fr.indicatorsvalue.IIndicatorsReportService;
//import com.plian.system.service.pm.property.IPropertyBasicsService;
//import com.plian.system.service.qg.subsidiary.ISubsidiaryMixedReformService;
//import com.plian.system.service.sys.orgzation.IOrgService;
//import com.plian.system.service.zzd.IZZDFinanceService;
//import com.plian.system.service.zzd.IZZDMixedReformService;
//import com.plian.system.vo.report.PieValueVO;
//import com.plian.system.vo.zzd.finance.FinancialDataVO;
//import com.plian.system.vo.zzd.finance.IndicatorsUnitDetailVO;
//import com.plian.system.vo.zzd.mixedReform.MixedAnnualVO;
//import com.plian.system.vo.zzd.mixedReform.MixedFinishVO;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Service;
//
//import java.math.BigDecimal;
//import java.math.RoundingMode;
//import java.util.*;
//import java.util.stream.Collectors;
//
///**
// * @Description: TODO
// * @Author: Take-off
// * @Date: 2023/3/15 4:22 PM
// * @Param
// * @return
// * @Version: 1.0
// **/
//@Slf4j
//@Service
//@AllArgsConstructor
//public class ZZDMixedReformServiceImpl implements IZZDMixedReformService {
//
//    private ISubsidiaryMixedReformService subsidiaryMixedReformService;
//
//    private IPropertyBasicsService propertyBasicsService;
//
//    private IOrgService orgService;
//
//    private IIndicatorsReportService indicatorsReportService;
//
//    private INewBulletinService newBulletinService;
//
//    private IBulletinValueService bulletinValueService;
//
//    private MixedFinishMonthMapper mixedFinishMonthMapper;
//
////    private IZZDFinanceService financeService;
//
//    private CompanyStokerightMapper companyStokerightMapper;
//
//    private MixedAnnualMonthMapper mixedAnnualMonthMapper;
//
//    //竞争类混改子企业组织形式
//    private final List<String> MIX_ORG_FORM = Arrays.asList("002001001","002001002","002001003","002001004","002001005");
//
//    private final static int OUT_STATUS = 4;
//
//    //资产总额 code
//    private final String ZCZE = "125023";
//    //利润总额 code
//    private final String LRZE = "125020";
//    //营业收入 code
//    private final String YYZSR = "125001";
//
//    private final BaseInfoDao baseInfoDao;
//
//    @Override
//    public MixedAnnualVO getMixedAnnual(ZZDQuery query){
//        MixedAnnualVO mixedAnnualVO = MixedAnnualVO.builder().mixedReformRate(BigDecimal.ZERO).mixedReformCompetition(0).assetSecuritizationRate(BigDecimal.ZERO).listedCompany(0).build();
//        String reportTime = query.getReportTime();
//        //组织Id
//        List<String> orgIds = new ArrayList<>();
//        if (query.getOrgType() == 1){
//            //全大市
//            orgIds = orgService.getAllOrg().stream().map(Org::getId).collect(Collectors.toList());
//        }else {
//            //监管企业
//            orgIds = orgService.getGzwUnits(orgService.getDTFlag(reportTime)).stream().map(Org::getId).collect(Collectors.toList());
//        }
//        //产权组织
//        Date reportDate = DateUtil.parse(reportTime, DateUtil.PATTERN_MONTH);
//        List<PropertyBasics> propertyList = propertyBasicsService.getAllChildByOrgIdsAndTime(orgIds, DateUtil.format(DateUtil.plusMonths(reportDate,1), DateUtil.PATTERN_DATE));
//        //混改组织
//        if (CollectionUtil.isNotEmpty(propertyList)){
//            List<String> prgIds = propertyList.stream().map(PropertyBasics::getId).collect(Collectors.toList());
//            //混改率
//            List<SubsidiaryMixedReform> mixedReforms = subsidiaryMixedReformService.list(new LambdaQueryWrapper<SubsidiaryMixedReform>().in(SubsidiaryMixedReform::getMixedEnterpriseId, prgIds)
//                    .le(SubsidiaryMixedReform::getImplementationTime, DateUtil.plusMonths(reportDate, 1)));
//
//            if (CollectionUtil.isNotEmpty(mixedReforms)) {
//                //一家企业取最新的
//                List<SubsidiaryMixedReform> reformList = getLastesMixOrg(mixedReforms);
//                //已引入社会资本
//                BigDecimal introduceCapitalAmount = reformList.stream().map(SubsidiaryMixedReform::getIntroduceCapitalAmount).filter(m -> Optional.ofNullable(m).isPresent()).reduce(BigDecimal.ZERO, BigDecimal::add);
//                BigDecimal nonpublicCapitalAmount = reformList.stream().map(SubsidiaryMixedReform::getNonpublicCapitalAmount).filter(m -> Optional.ofNullable(m).isPresent()).reduce(BigDecimal.ZERO, BigDecimal::add);
//                BigDecimal socialCapital = introduceCapitalAmount.add(nonpublicCapitalAmount);
//                mixedAnnualVO.setSocialCapital(socialCapital.divide(new BigDecimal("10000"),4,RoundingMode.HALF_UP));
//            }else {
//                mixedAnnualVO.setSocialCapital(BigDecimal.ZERO);
//            }
//        }
//        //资产证券化 ：资产证券化率=（控股上市公司净资产+宁波港折股净资产+重要参股上市公司（持股5%及以上）折股净资产）/（监管企业净资产+省海港集团折股净资产）
//        List<CompanyStokeright> companyStokerights = companyStokerightMapper.selectList(null);
//        if (CollectionUtil.isNotEmpty(companyStokerights)) {
//            Map<String, List<CompanyStokeright>> mapByTime = companyStokerights.stream().collect(Collectors.groupingBy(m -> m.getYear() + "_" + m.getMonth()));
//            List<CompanyStokeright> stokerights = mapByTime.get(mapByTime.keySet().stream().max(Comparator.naturalOrder()).get());
//            //控股上市公司净资产
//            BigDecimal netAssets = stokerights.stream().filter(m -> "控股".equals(m.getType()) && !"宁波港".equals(m.getShortName())).map(m -> new BigDecimal(m.getNetAssets())).reduce(BigDecimal.ZERO, BigDecimal::add);
//            //宁波港折股净资产
//            BigDecimal nbAssets = stokerights.stream().filter(m -> "宁波港".equals(m.getShortName())).map(m -> new BigDecimal(m.getFoldNetAssets())).reduce(BigDecimal.ZERO, BigDecimal::add);
//            //重要参股上市公司折股净资产
//            BigDecimal foldNetAssets = stokerights.stream().filter(m -> "参股".equals(m.getType())).map(m -> new BigDecimal(m.getFoldNetAssets())).reduce(BigDecimal.ZERO, BigDecimal::add);
//            //监管企业净资产 + 省海港集团折股净资产
//            BigDecimal specialNet = stokerights.stream().filter(m -> ("监管企业净资产 ".equals(m.getShortName()) || "省海港集团".equals(m.getShortName())) && (Optional.ofNullable(m.getFoldNetAssets()).isPresent())).map(m -> new BigDecimal(m.getFoldNetAssets())).reduce(BigDecimal.ZERO, BigDecimal::add);
//            BigDecimal assetSecuritizationRate = BigDecimal.ZERO;
//            if (specialNet.compareTo(BigDecimal.ZERO) >0){
//                assetSecuritizationRate = ((netAssets.add(nbAssets).add(foldNetAssets)).multiply(new BigDecimal(100))).divide(specialNet, 2, RoundingMode.HALF_UP);
//
//            }
//            mixedAnnualVO.setAssetSecuritizationRate(assetSecuritizationRate);
//            mixedAnnualVO.setListedCompany(stokerights.size() -1);
//        }
//        String year = reportTime.split("-")[0];
//        String month = reportTime.split("-")[1];
//        List<MixedAnnualMonth> mixedAnnualMonths = mixedAnnualMonthMapper.selectList(new LambdaQueryWrapper<MixedAnnualMonth>().eq(MixedAnnualMonth::getYear, year).eq(MixedAnnualMonth::getMonth, month));
//        MixedAnnualMonth mixedAnnualMonth = new MixedAnnualMonth();
//        if (CollectionUtil.isNotEmpty(mixedAnnualMonths)){
//            mixedAnnualMonth = mixedAnnualMonths.get(0);
//        }else {
//            mixedAnnualMonth = mixedAnnualMonthMapper.selectList(new LambdaQueryWrapper<MixedAnnualMonth>().eq(MixedAnnualMonth::getYear, year)).stream().sorted(Comparator.comparing(MixedAnnualMonth::getMonth).reversed()).findFirst().get();
//        }
//        mixedAnnualVO.setMixedReformRate(mixedAnnualMonth.getMixedReformRate());
//        mixedAnnualVO.setMixedReformTotal(mixedAnnualMonth.getMixedReformTotal());
//        mixedAnnualVO.setMixedReformCompetition(mixedAnnualMonth.getMixedReformCompetition());
//        return mixedAnnualVO;
//    }
//
//    /**
//     * @Description: //TODO 获取最新混改数据
//     * @Author: Take-off
//     * @Date: 10:58 AM 2023/5/31
//     * @Param: [mixedReforms]
//     * @return: java.util.List<com.plian.system.entity.qg.subsidiary.SubsidiaryMixedReform>
//     **/
//    private List<SubsidiaryMixedReform> getLastesMixOrg(List<SubsidiaryMixedReform> mixedReforms){
//        List<SubsidiaryMixedReform> reforms = new ArrayList<>();
//
//        Map<String, List<SubsidiaryMixedReform>> mapByOrg = mixedReforms.stream().filter(m -> Optional.ofNullable(m.getMixedEnterpriseId()).isPresent()).collect(Collectors.groupingBy(SubsidiaryMixedReform::getMixedEnterpriseId));
//        for (String orgId:mapByOrg.keySet()){
//            mapByOrg.get(orgId).stream().max(Comparator.comparing(SubsidiaryMixedReform::getUpdateTime)).ifPresent(reforms::add);
//        }
//        return reforms;
//    }
//
//    @Override
//    public MixedAnnualVO getMixedTotal(String date){
//        MixedAnnualVO mixedAnnualVO = new MixedAnnualVO();
//        if (StringUtil.isBlank(date)){
//            date = minDate();
//        }
//        List<String> orgIds = orgService.getGzwUnits(orgService.getDTFlag(date)).stream().map(Org::getId).collect(Collectors.toList());
//        //产权组织
//        List<PropertyBasics> propertyList = propertyBasicsService.getAllChildByOrgIdsAndTime(orgIds,date);
//        //混改组织
//        if (CollectionUtil.isNotEmpty(propertyList)) {
//            List<String> prgIds = propertyList.stream().map(PropertyBasics::getId).collect(Collectors.toList());
//            //混改率
//            List<SubsidiaryMixedReform> mixedReforms = subsidiaryMixedReformService.list(new LambdaQueryWrapper<SubsidiaryMixedReform>().in(SubsidiaryMixedReform::getMixedEnterpriseId, prgIds)
//                    .eq(SubsidiaryMixedReform::getIsLatest,2)
//                    .lt(SubsidiaryMixedReform::getImplementationTime,DateUtil.plusMonths(DateUtil.parse(date,DateUtil.PATTERN_MONTH),1)));
//            if (CollectionUtil.isNotEmpty(mixedReforms)) {
//                //一家企业取最新的
//                List<SubsidiaryMixedReform> reformList = getLastesMixOrg(mixedReforms);
//                //已引入社会资本
//                BigDecimal introduceCapitalAmount = reformList.stream().map(SubsidiaryMixedReform::getIntroduceCapitalAmount).filter(m -> Optional.ofNullable(m).isPresent()).reduce(BigDecimal.ZERO, BigDecimal::add);
//                BigDecimal nonpublicCapitalAmount = reformList.stream().map(SubsidiaryMixedReform::getNonpublicCapitalAmount).filter(m -> Optional.ofNullable(m).isPresent()).reduce(BigDecimal.ZERO, BigDecimal::add);
//                BigDecimal socialCapital = introduceCapitalAmount.add(nonpublicCapitalAmount);
//                mixedAnnualVO.setSocialCapital(socialCapital.divide(new BigDecimal("10000"),4,RoundingMode.HALF_UP));
//            }
//        }
//        String year = date.split("-")[0];
//        String month = date.split("-")[1];
//        List<MixedAnnualMonth> mixedAnnualMonths = mixedAnnualMonthMapper.selectList(new LambdaQueryWrapper<MixedAnnualMonth>().eq(MixedAnnualMonth::getYear, year).eq(MixedAnnualMonth::getMonth, month));
//        MixedAnnualMonth mixedAnnualMonth = new MixedAnnualMonth();
//        if (CollectionUtil.isNotEmpty(mixedAnnualMonths)){
//            mixedAnnualMonth = mixedAnnualMonths.get(0);
//        }else {
//            mixedAnnualMonth = mixedAnnualMonthMapper.selectList(new LambdaQueryWrapper<MixedAnnualMonth>().eq(MixedAnnualMonth::getYear, year)).stream().sorted(Comparator.comparing(MixedAnnualMonth::getMonth).reversed()).findFirst().get();
//        }
//        mixedAnnualVO.setMixedReformRate(mixedAnnualMonth.getMixedReformRate());
//        mixedAnnualVO.setMixedReformTotal(mixedAnnualMonth.getMixedReformTotal());
//        mixedAnnualVO.setMixedReformCompetition(mixedAnnualMonth.getMixedReformCompetition());
//        return mixedAnnualVO;
//    }
//
//    @Override
//    public List<MixedFinishVO> getMixedFinish(String date){
//        List<MixedFinishVO> mixedFinishVOS = new ArrayList<>();
//
//        if (StringUtil.isBlank(date)){
//            date = minDate();
//        }
//        String year = date.split("-")[0];
//        String month = date.split("-")[1];
//        List<MixedFinishMonth> mixedFinishMonths = mixedFinishMonthMapper.selectList(new LambdaQueryWrapper<MixedFinishMonth>().eq(MixedFinishMonth::getYear, year).eq(MixedFinishMonth::getMonth, month)
//                .isNotNull(MixedFinishMonth::getOrgType));
//        if (CollectionUtil.isNotEmpty(mixedFinishMonths)){
//            mixedFinishVOS = getMixedFinishVO(mixedFinishMonths);
//        }
//        return mixedFinishVOS;
//    }
//
//    @Override
//    public List<MixedAnnualVO> getSocialCapital(String date){
//        if (StringUtil.isBlank(date)){
//            date = minDate();
//        }
//        List<MixedAnnualVO> mixedAnnualVOS = new ArrayList<>();
//        //获取四个季度
//        String year = DateUtil.format(new Date(), DateUtil.PATTERN_YEAR);
//        Date firstTime = DateUtil.parse(year+"-01",DateUtil.PATTERN_MONTH);
//        Date secondTime = DateUtil.parse(year+"-04",DateUtil.PATTERN_MONTH);
//        Date thirdTime = DateUtil.parse(year+"-07",DateUtil.PATTERN_MONTH);
//        Date fourthTime = DateUtil.parse(year+"-10",DateUtil.PATTERN_MONTH);
//        Date fifthTime = DateUtil.plusYears(firstTime,1);
//        //混改率
//        List<SubsidiaryMixedReform> mixedReforms = subsidiaryMixedReformService.list(new LambdaQueryWrapper<SubsidiaryMixedReform>().eq(SubsidiaryMixedReform::getStatus,3)
//                .eq(SubsidiaryMixedReform::getIsLatest,2));
//        if (CollectionUtil.isNotEmpty(mixedReforms)) {
//            //一家企业取最新的
//            List<SubsidiaryMixedReform> reformList = getLastesMixOrg(mixedReforms);
//            List<SubsidiaryMixedReform> firstReforms = reformList.stream().filter(m -> Optional.ofNullable(m.getCreateTime()).isPresent() && firstTime.before(m.getCreateTime()) && secondTime.after(m.getCreateTime()))
//                    .collect(Collectors.toList());
//            MixedAnnualVO firstAnnual = MixedAnnualVO.builder().socialCapital(addSocial(firstReforms).divide(new BigDecimal("10000"),4,RoundingMode.HALF_UP)).tag("第一季度").build();
//            mixedAnnualVOS.add(firstAnnual);
//
//            List<SubsidiaryMixedReform> secondReforms = reformList.stream().filter(m -> Optional.ofNullable(m.getImplementationTime()).isPresent()
//                            && secondTime.before(m.getImplementationTime()) && thirdTime.after(m.getImplementationTime()))
//                    .collect(Collectors.toList());
//            MixedAnnualVO secondAnnual = MixedAnnualVO.builder().socialCapital(addSocial(secondReforms).divide(new BigDecimal("10000"),4,RoundingMode.HALF_UP)).tag("第二季度").build();
//            mixedAnnualVOS.add(secondAnnual);
//
//            List<SubsidiaryMixedReform> thirdReforms = reformList.stream().filter(m -> Optional.ofNullable(m.getImplementationTime()).isPresent()
//                            && thirdTime.before(m.getImplementationTime()) && fourthTime.after(m.getImplementationTime()))
//                    .collect(Collectors.toList());
//            MixedAnnualVO thirdAnnual = MixedAnnualVO.builder().socialCapital(addSocial(thirdReforms).divide(new BigDecimal("10000"),4,RoundingMode.HALF_UP)).tag("第三季度").build();
//            mixedAnnualVOS.add(thirdAnnual);
//
//            List<SubsidiaryMixedReform> fourthReforms = reformList.stream().filter(m -> Optional.ofNullable(m.getImplementationTime()).isPresent()
//                            && fourthTime.before(m.getImplementationTime()) && fifthTime.after(m.getImplementationTime()))
//                    .collect(Collectors.toList());
//            MixedAnnualVO fourthAnnual = MixedAnnualVO.builder().socialCapital(addSocial(fourthReforms).divide(new BigDecimal("10000"),4,RoundingMode.HALF_UP)).tag("第四季度").build();
//            mixedAnnualVOS.add(fourthAnnual);
//        }
//        return mixedAnnualVOS;
//    }
//
//    @Override
//    public List<MixedFinishVO> getOrgSort(Integer sortType,Integer isThree,String date){
//
//        if (StringUtil.isBlank(date)){
//            date = minDate();
//        }
//        List<MixedFinishVO> mixedFinishVOS = new ArrayList<>();
//
//        //查询混改企业的组织id
//        String year = date.split("-")[0];
//        String month = date.split("-")[1];
//        List<MixedFinishMonth> mixedFinishMonths = mixedFinishMonthMapper.selectList(new LambdaQueryWrapper<MixedFinishMonth>().eq(MixedFinishMonth::getYear, year).eq(MixedFinishMonth::getMonth, month)
//                .isNull(MixedFinishMonth::getOrgType));
//        if (CollectionUtil.isNotEmpty(mixedFinishMonths)){
//            mixedFinishVOS = getMixedFinishVO(mixedFinishMonths);
//        }
//        if (isThree == 1){
//            List<SubsidiaryMixedReform> mixedReforms = subsidiaryMixedReformService.list(new LambdaQueryWrapper<SubsidiaryMixedReform>()
//                            .eq(SubsidiaryMixedReform::getStatus,3).eq(SubsidiaryMixedReform::getIsLatest,2).eq(SubsidiaryMixedReform::getIsThree,1));
//            if (CollectionUtil.isNotEmpty(mixedReforms)){
//                //一家企业取最新的
//                List<SubsidiaryMixedReform> reformList = getLastesMixOrg(mixedReforms);
//                List<String> proIds = reformList.stream().map(SubsidiaryMixedReform::getMixedEnterpriseId).collect(Collectors.toList());
//                if (CollectionUtil.isNotEmpty(proIds)){
//                    List<PropertyBasics> propertyBasics = propertyBasicsService.listByIds(proIds);
//                    List<String> orgIds = propertyBasics.stream().map(PropertyBasics::getOrgId).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList());
//                    if (CollectionUtil.isNotEmpty(mixedFinishVOS)){
//                        mixedFinishVOS = mixedFinishVOS.stream().filter(m -> orgIds.contains(m.getOrgId())).collect(Collectors.toList());
//                    }
//                }
//            }else {
//                mixedFinishVOS = new ArrayList<>();
//            }
//        }
//        //排序
//        switch (sortType){
//            case 1:mixedFinishVOS = mixedFinishVOS.stream().filter(m -> Optional.ofNullable(m.getTotalAssets()).isPresent()).sorted(Comparator.comparing(MixedFinishVO::getTotalAssets).reversed()).collect(Collectors.toList());break;
//            case 2:mixedFinishVOS = mixedFinishVOS.stream().filter(m -> Optional.ofNullable(m.getTotalProfit()).isPresent()).sorted(Comparator.comparing(MixedFinishVO::getTotalProfit).reversed()).collect(Collectors.toList());break;
//            case 3:mixedFinishVOS = mixedFinishVOS.stream().filter(m -> Optional.ofNullable(m.getNetAssetRate()).isPresent()).sorted(Comparator.comparing(MixedFinishVO::getNetAssetRate).reversed()).collect(Collectors.toList());break;
//            default:break;
//        }
//        return mixedFinishVOS;
//    }
//
//    @Override
//    public List<PieValueVO> getOrgOrder(String date){
//        if (StringUtil.isBlank(date)){
//            date = minDate();
//        }
//        List<PieValueVO> pieValueVOS = new ArrayList<>();
//        List<String> orgIds = orgService.getGzwUnits(orgService.getDTFlag(date)).stream().map(Org::getId).collect(Collectors.toList());
//        //产权组织
//        List<PropertyBasics> propertyList = propertyBasicsService.getAllChildByOrgIdsAndTime(orgIds,date);
//        //混改企业
//        List<SubsidiaryMixedReform> mixedReforms = subsidiaryMixedReformService.list(new LambdaQueryWrapper<SubsidiaryMixedReform>()
//                .eq(SubsidiaryMixedReform::getStatus,3).eq(SubsidiaryMixedReform::getIsLatest,2));
//        if (CollectionUtil.isNotEmpty(mixedReforms)){
//            //一家企业取最新的
//            List<SubsidiaryMixedReform> reformList = getLastesMixOrg(mixedReforms);
//            //混改产权id
//            List<String> mixProIds = reformList.stream().map(SubsidiaryMixedReform::getMixedEnterpriseId).collect(Collectors.toList());
//            List<PropertyBasics> mixPropertys = propertyList.stream().filter(m -> mixProIds.contains(m.getId())).collect(Collectors.toList());
//            Map<String, List<PropertyBasics>> mapByOrgGradation = mixPropertys.stream().filter(m -> Optional.ofNullable(m.getOrgGradation()).isPresent()).collect(Collectors.groupingBy(PropertyBasics::getOrgGradation));
//            pieValueVOS.add(PieValueVO.builder().name("一级").value(CollectionUtil.isNotEmpty(mapByOrgGradation.get("001001")) ? mapByOrgGradation.get("001001").size() :0 ).build());
//            pieValueVOS.add(PieValueVO.builder().name("二级").value(CollectionUtil.isNotEmpty(mapByOrgGradation.get("001002")) ? mapByOrgGradation.get("001002").size() :0 ).build());
//            pieValueVOS.add(PieValueVO.builder().name("三级").value(CollectionUtil.isNotEmpty(mapByOrgGradation.get("001003")) ? mapByOrgGradation.get("001003").size() :0 ).build());
//            pieValueVOS.add(PieValueVO.builder().name("四级").value(CollectionUtil.isNotEmpty(mapByOrgGradation.get("001004")) ? mapByOrgGradation.get("001004").size() :0 ).build());
//        }
//        return pieValueVOS;
//    }
//
//    @Override
//    public List<PieValueVO> getMixTypes(String date){
//        if (StringUtil.isBlank(date)){
//            date = minDate();
//        }
//        //混改方式
//        List<PageData> findbycodes = new ArrayList<>();
//        try {
//            findbycodes = baseInfoDao.findbyParentid("044");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        List<PieValueVO> pieValueVOS = new ArrayList<>();
//        //混改企业
//        List<SubsidiaryMixedReform> mixedReforms = subsidiaryMixedReformService.list(new LambdaQueryWrapper<SubsidiaryMixedReform>()
//                .eq(SubsidiaryMixedReform::getStatus,3).eq(SubsidiaryMixedReform::getIsLatest,2));
//        if (CollectionUtil.isNotEmpty(mixedReforms)){
//            //一家企业取最新的
//            List<SubsidiaryMixedReform> reformList = getLastesMixOrg(mixedReforms);
//            Map<String, Integer> modeMap = new HashMap<>();
//            for (SubsidiaryMixedReform mixedReform:reformList){
//                String mixedModeId = mixedReform.getMixedModeId();
//                if (StringUtil.isNotBlank(mixedModeId)){
//                    String[] mixedModeIds = mixedModeId.split(",");
//                    for (String modeId:mixedModeIds){
//                        if (modeMap.containsKey(modeId)){
//                            modeMap.put(modeId,modeMap.get(modeId) +1);
//                        }else {
//                            modeMap.put(modeId,1);
//                        }
//                    }
//                }
//            }
//            Set<String> modeIds = modeMap.keySet();
//            if (CollectionUtil.isNotEmpty(modeIds)){
//                for (String modeId:modeIds){
//                    Optional<PageData> code = findbycodes.stream().filter(m -> modeId.equals(m.getString("code"))).findFirst();
//                    code.ifPresent(pageData ->
//                            pieValueVOS.add(PieValueVO.builder().name(pageData.getString("name")).value(modeMap.get(modeId)).build()));
//                }
//            }
//
//        }
//        return pieValueVOS;
//    }
//
//    @Override
//    public void saveAllMixedValue(String date){
//        if (StringUtil.isBlank(date)){
//            date = minDate();
//        }
//        List<MixedFinishVO> mixedFinishVOS = new ArrayList<>();
//        //所有组织
//        List<Org> orgList = orgService.list(new LambdaQueryWrapper<Org>().ne(Org::getIsDel,1));
//        List<String> parentIds = orgList.stream().map(Org::getParentId).collect(Collectors.toList());
//        //产权组织
//        List<PropertyBasics> propertyList = propertyBasicsService.list(Wrappers.<PropertyBasics>lambdaQuery().ne(PropertyBasics::getStatus, OUT_STATUS));
//        //混改企业
//        List<SubsidiaryMixedReform> mixedReforms = subsidiaryMixedReformService.list(new LambdaQueryWrapper<SubsidiaryMixedReform>()
//                .eq(SubsidiaryMixedReform::getStatus,3).eq(SubsidiaryMixedReform::getIsLatest,2));
//
//        List<String> mixProIds = mixedReforms.stream().map(SubsidiaryMixedReform::getMixedEnterpriseId).collect(Collectors.toList());
////        List<String> mixOrgIds = propertyList.stream().filter(m -> !mixProIds.contains(m.getId())).map(PropertyBasics::getOrgId).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList());
//        List<String> mixOrgIds = propertyBasicsService.list(new LambdaQueryWrapper<PropertyBasics>().eq(PropertyBasics::getIsMix, 1).ne(PropertyBasics::getStatus, OUT_STATUS))
//                .stream().map(PropertyBasics::getOrgId).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList());
//        List<IndicatorsUnitDetailVO> groupNetAssets = indicatorsReportService.getAllNetAssets(mixOrgIds.stream().filter(parentIds::contains).collect(Collectors.toList()),
//                mixOrgIds.stream().filter(m -> !parentIds.contains(m)).collect(Collectors.toList()),
//                date);
//        for (IndicatorsUnitDetailVO indicatorsUnitDetailVO :groupNetAssets){
//            mixedFinishVOS.add(MixedFinishVO.builder().orgId(indicatorsUnitDetailVO.getUnitId()).orgName(indicatorsUnitDetailVO.getUnitName()).netAssetRate(indicatorsUnitDetailVO.getValue()).build());
//        }
//        //计算快报值
//        setFinishValue(date,mixedFinishVOS);
//
//        MixedFinishVO mixedFinish = MixedFinishVO.builder().orgType(1).build();
//        mixedFinish.setNetAssetRate(indicatorsReportService.getNetAssets(mixOrgIds.stream().filter(parentIds::contains).collect(Collectors.toList()),
//                mixOrgIds.stream().filter(m -> !parentIds.contains(m)).collect(Collectors.toList()),
//                date));
//        //取快报数据
//        setFinishValue(mixOrgIds,date, mixedFinish);
//        mixedFinishVOS.add(mixedFinish);
//        //竞争类混改子企业
//        List<PropertyBasics> mixCompetitions = propertyList.stream().filter(m -> Optional.ofNullable(m.getFunctionalCategoryName()).isPresent() && "043002".equals(m.getFunctionalCategoryName())
//                && Optional.ofNullable(m.getIsMix()).isPresent() && "1".equals(m.getIsMix())
//                && Optional.ofNullable(m.getNature()).isPresent() && "0".equals(m.getNature())
//                && Optional.ofNullable(m.getIsGroup()).isPresent() && "1".equals(m.getIsGroup())
//                && Optional.ofNullable(m.getOrganizationForm()).isPresent() && MIX_ORG_FORM.contains(m.getOrganizationForm())
//        ).collect(Collectors.toList());
//        List<String> mixCompetitionOrgIds = mixCompetitions.stream().filter(m -> !mixProIds.contains(m.getId())).map(PropertyBasics::getOrgId).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList());
//        MixedFinishVO mixedCompetition = MixedFinishVO.builder().orgType(2).build();
//
//        mixedCompetition.setNetAssetRate(indicatorsReportService.getNetAssets(mixCompetitionOrgIds.stream().filter(parentIds::contains).collect(Collectors.toList()),
//                mixCompetitionOrgIds.stream().filter(m -> !parentIds.contains(m)).collect(Collectors.toList()),
//                date)
//        );
//        //取快报数据
//        setFinishValue(mixCompetitionOrgIds,date, mixedCompetition);
//        mixedFinishVOS.add(mixedCompetition);
//        //直接监管企业
//        List<String> units = orgService.getGzwUnits(orgService.getDTFlag(DateUtil.format(new Date(),DateUtil.PATTERN_MONTH))).stream().map(Org::getId).collect(Collectors.toList());
//        MixedFinishVO unitCompetition = MixedFinishVO.builder().orgType(3).build();
//        ZZDQuery zzdQuery = new ZZDQuery();
//        zzdQuery.setReportTime(date);
//        zzdQuery.setOrgType(2);
//        FinancialDataVO financialDataVO = financeService.financialData(zzdQuery);
//        if (Optional.ofNullable(financialDataVO).isPresent()){
//            unitCompetition.setNetAssetRate(financialDataVO.getNetAssetsRoe());
//            unitCompetition.setTotalAssets(financialDataVO.getTotalAssets());
//            unitCompetition.setTotalProfit(financialDataVO.getTotalProfit());
//            unitCompetition.setBusinessIncome(financialDataVO.getBusinessIncome());
//        }
//        mixedFinishVOS.add(unitCompetition);
//        List<MixedFinishMonth> mixedFinishMonths = getMixedFinishMonth(mixedFinishVOS,date);
//        saveMixedFinishMonth(mixedFinishMonths,date);
//
//    }
//
//    @Override
//    public void saveAllMixedAnnual(String date){
//        MixedAnnualMonth mixedAnnualMonth = new MixedAnnualMonth();
//        String year = date.split("-")[0];
//        String month = date.split("-")[1];
//        mixedAnnualMonth.setYear(year);
//        mixedAnnualMonth.setMonth(month);
//        mixedAnnualMonth.setIsDeleted(0);
//        mixedAnnualMonth.setCreateTime(DateUtil.now());
//        mixedAnnualMonthMapper.insert(mixedAnnualMonth);
//    }
//    /**
//     * @Description: //TODO 计算社会资本
//     * @Author: Take-off
//     * @Date: 5:43 PM 2023/3/16
//     * @Param: [mixedReforms]
//     * @return: java.math.BigDecimal
//     **/
//    private BigDecimal addSocial(List<SubsidiaryMixedReform> mixedReforms){
//        BigDecimal introduceCapitalAmount = mixedReforms.stream().map(SubsidiaryMixedReform::getIntroduceCapitalAmount).filter(m -> Optional.ofNullable(m).isPresent()).reduce(BigDecimal.ZERO, BigDecimal::add);
//        BigDecimal nonpublicCapitalAmount = mixedReforms.stream().map(SubsidiaryMixedReform::getNonpublicCapitalAmount).filter(m -> Optional.ofNullable(m).isPresent()).reduce(BigDecimal.ZERO, BigDecimal::add);
//        BigDecimal socialCapital = introduceCapitalAmount.add(nonpublicCapitalAmount);
//        return socialCapital;
//    }
//
//    /**
//     * @Description: //TODO 计算快报值
//     * @Author: Take-off
//     * @Date: 4:39 PM 2023/3/16
//     * @Param: [orgIds, date, mixedCompetition]
//     * @return: void
//     **/
//    private void setFinishValue( List<String> orgIds,String date, MixedFinishVO mixedCompetition){
//        if (CollectionUtil.isNotEmpty(orgIds)){
//            //快报主表
//            List<NewBulletin> bulletins = newBulletinService.list(new LambdaQueryWrapper<NewBulletin>().in(NewBulletin::getOrgId, orgIds).eq(NewBulletin::getStatus, 3)
//                    .eq(NewBulletin::getReportTime, date+"-01"));
//            if (CollectionUtil.isNotEmpty(bulletins)){
//                List<String> bulletinIds = new ArrayList<>();
//                for (String orgId:orgIds){
//                    List<NewBulletin> bulletinList = bulletins.stream().filter(m -> orgId.equals(m.getOrgId())).collect(Collectors.toList());
//                    if (CollectionUtil.isNotEmpty(bulletinList)){
//                        Optional<NewBulletin> first = bulletinList.stream().filter(m -> "6".equals(m.getTableTypeCode())).findFirst();
//                        if (first.isPresent()) {
//                            bulletinIds.add(first.get().getId());
//                        }else {
//                            bulletinList.stream().filter(m -> "0".equals(m.getTableTypeCode())).findFirst().ifPresent(m -> bulletinIds.add(m.getId()));
//                        }
//                    }
//                }
//                //快报值
//                List<BulletinValue> bulletinValues = bulletinValueService.list(new LambdaQueryWrapper<BulletinValue>().in(BulletinValue::getBulletinId, bulletinIds));
//                if (CollectionUtil.isNotEmpty(bulletinValues)){
//                    BigDecimal totalAssets = bulletinValues.stream().filter(m -> ZCZE.equals(m.getTypeCode())).map(BulletinValue::getYearAmount).filter(m -> Optional.ofNullable(m).isPresent())
//                            .reduce(BigDecimal.ZERO, BigDecimal::add);
//                    mixedCompetition.setTotalAssets(totalAssets.divide(new BigDecimal(10000),2,RoundingMode.HALF_UP));
//
//                    BigDecimal totalProfit = bulletinValues.stream().filter(m -> LRZE.equals(m.getTypeCode())).map(BulletinValue::getYearAmount).filter(m -> Optional.ofNullable(m).isPresent())
//                            .reduce(BigDecimal.ZERO, BigDecimal::add);
//                    mixedCompetition.setTotalProfit(totalProfit.divide(new BigDecimal(10000),2,RoundingMode.HALF_UP));
//                }else {
//                    mixedCompetition.setTotalAssets(BigDecimal.ZERO);
//                    mixedCompetition.setTotalProfit(BigDecimal.ZERO);
//                    mixedCompetition.setBusinessIncome(BigDecimal.ZERO);
//                }
//            }
//        }else {
//            mixedCompetition.setTotalAssets(BigDecimal.ZERO);
//            mixedCompetition.setTotalProfit(BigDecimal.ZERO);
//            mixedCompetition.setBusinessIncome(BigDecimal.ZERO);
//        }
//    }
//
//    /**
//     * @Description: //TODO 计算快报值
//     * @Author: Take-off
//     * @Date: 6:27 PM 2023/3/16
//     * @Param: [orgIds, date, mixedFinishVOS]
//     * @return: void
//     **/
//    @Override
//    public void setFinishValue(String date, List<MixedFinishVO> mixedFinishVOS){
//        List<String> orgIds = mixedFinishVOS.stream().map(MixedFinishVO::getOrgId).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList());
//        if (CollectionUtil.isNotEmpty(orgIds)) {
//            //快报主表
//            List<NewBulletin> bulletins = newBulletinService.list(new LambdaQueryWrapper<NewBulletin>().in(NewBulletin::getOrgId, orgIds)
//                    .eq(NewBulletin::getReportTime, date + "-01"));
//            if (CollectionUtil.isNotEmpty(bulletins)) {
//                //快报值
//                List<BulletinValue> bulletinValues = bulletinValueService.list(new LambdaQueryWrapper<BulletinValue>().in(BulletinValue::getBulletinId, bulletins.stream().map(NewBulletin::getId).collect(Collectors.toList())));
//                if (CollectionUtil.isNotEmpty(bulletins)) {
//                    List<String> bulletinIds = new ArrayList<>();
//                    for (MixedFinishVO mixedFinishVO : mixedFinishVOS) {
//                        if (Optional.ofNullable( mixedFinishVO.getOrgId()).isPresent()){
//                            List<NewBulletin> bulletinList = bulletins.stream().filter(m -> mixedFinishVO.getOrgId().equals(m.getOrgId())).collect(Collectors.toList());
//                            if (CollectionUtil.isNotEmpty(bulletinList)) {
//                                Optional<NewBulletin> first = bulletinList.stream().filter(m -> "6".equals(m.getTableTypeCode())).findFirst();
//                                String bulletinId = "";
//                                if (first.isPresent()) {
//                                    bulletinId = first.get().getId();
//                                } else {
//                                    Optional<NewBulletin> bulletin = bulletinList.stream().filter(m -> "0".equals(m.getTableTypeCode())).findFirst();
//                                    if (bulletin.isPresent()) {
//                                        bulletinId = bulletin.get().getId();
//                                    }
//                                }
//                                if (StringUtil.isNotBlank(bulletinId)) {
//                                    for (BulletinValue bulletinValue : bulletinValues) {
//                                        if (bulletinId.equals(bulletinValue.getBulletinId())) {
//                                            if (ZCZE.equals(bulletinValue.getTypeCode())) {
//                                                mixedFinishVO.setTotalAssets(bulletinValue.getYearAmount());
//                                            } else if (LRZE.equals(bulletinValue.getTypeCode())) {
//                                                mixedFinishVO.setTotalProfit(bulletinValue.getYearAmount());
//                                            } else if (YYZSR.equals(bulletinValue.getTypeCode())) {
//                                                mixedFinishVO.setBusinessIncome(bulletinValue.getYearAmount());
//                                            }
//                                        }
//                                    }
//                                } else {
//                                    mixedFinishVO.setTotalAssets(BigDecimal.ZERO);
//                                    mixedFinishVO.setTotalProfit(BigDecimal.ZERO);
//                                    mixedFinishVO.setBusinessIncome(BigDecimal.ZERO);
//                                }
//                            } else {
//                                mixedFinishVO.setTotalAssets(BigDecimal.ZERO);
//                                mixedFinishVO.setTotalProfit(BigDecimal.ZERO);
//                                mixedFinishVO.setBusinessIncome(BigDecimal.ZERO);
//                            }
//                        }
//                    }
//                }
//            } else {
//                mixedFinishVOS.forEach(mixedFinishVO -> {
//                    mixedFinishVO.setTotalAssets(BigDecimal.ZERO);
//                    mixedFinishVO.setTotalProfit(BigDecimal.ZERO);
//                    mixedFinishVO.setBusinessIncome(BigDecimal.ZERO);
//                });
//            }
//        }
//    }
//
//    /**
//     * @Description: //TODO 批量保存混改财务数据
//     * @Author: Take-off
//     * @Date: 11:05 AM 2023/3/24
//     * @Param: [mixedFinishMonths, date]
//     * @return: void
//     **/
//    private void saveMixedFinishMonth(List<MixedFinishMonth> mixedFinishMonths,String date){
//        String year = date.split("-")[0];
//        String month = date.split("-")[1];
//        if (CollectionUtil.isNotEmpty(mixedFinishMonths)){
//            //删除老数据
//            mixedFinishMonthMapper.delete(new LambdaQueryWrapper<MixedFinishMonth>().eq(MixedFinishMonth::getYear, year).eq(MixedFinishMonth::getMonth, month));
//            mixedFinishMonths.forEach(mixedFinishMonth -> {
//                        mixedFinishMonth.setIsDeleted(0);
//                        mixedFinishMonth.setCreateTime(DateUtil.now());
//                mixedFinishMonthMapper.insert(mixedFinishMonth);
//            }
//            );
//        }
//    }
//
//    /**
//     * @Description: //TODO 获取混改财务计算值
//     * @Author: Take-off
//     * @Date: 10:57 AM 2023/3/24
//     * @Param: [mixedFinishVOS, date]
//     * @return: java.util.List<com.plian.system.entity.bi.MixedFinishMonth>
//     **/
//    private List<MixedFinishMonth> getMixedFinishMonth(List<MixedFinishVO> mixedFinishVOS,String date){
//        List<MixedFinishMonth> mixedFinishMonths = new ArrayList<>();
//        String year = date.split("-")[0];
//        String month = date.split("-")[1];
//
//        mixedFinishVOS.forEach( mixedFinishVO -> {
//            MixedFinishMonth mixedFinishMonth = new MixedFinishMonth();
//            mixedFinishMonth.setOrgId(mixedFinishVO.getOrgId());
//            mixedFinishMonth.setOrgType(mixedFinishVO.getOrgType());
//            mixedFinishMonth.setOrgName(mixedFinishVO.getOrgName());
//            mixedFinishMonth.setTotalAssets(mixedFinishVO.getTotalAssets());
//            mixedFinishMonth.setNetAssetRate(mixedFinishVO.getNetAssetRate());
//            mixedFinishMonth.setTotalProfit(mixedFinishVO.getTotalProfit());
//            mixedFinishMonth.setBusinessIncome(mixedFinishVO.getBusinessIncome());
//            mixedFinishMonth.setYear(year);
//            mixedFinishMonth.setMonth(month);
//            mixedFinishMonths.add(mixedFinishMonth);
//        });
//        return mixedFinishMonths;
//    }
//
//    private List<MixedFinishVO> getMixedFinishVO(List<MixedFinishMonth> mixedFinishMonths){
//        List<MixedFinishVO> mixedFinishVOS = new ArrayList<>();
//
//        mixedFinishMonths.forEach( mixedFinishMonth -> {
//            MixedFinishVO mixedFinishVO = new MixedFinishVO();
//            mixedFinishVO.setOrgId(mixedFinishMonth.getOrgId());
//            mixedFinishVO.setOrgType(mixedFinishMonth.getOrgType());
//            mixedFinishVO.setOrgName(mixedFinishMonth.getOrgName());
//            mixedFinishVO.setTotalAssets(mixedFinishMonth.getTotalAssets());
//            mixedFinishVO.setNetAssetRate(mixedFinishMonth.getNetAssetRate());
//            mixedFinishVO.setTotalProfit(mixedFinishMonth.getTotalProfit());
//            mixedFinishVO.setBusinessIncome(mixedFinishMonth.getBusinessIncome());
//            mixedFinishVOS.add(mixedFinishVO);
//        });
//        return mixedFinishVOS;
//    }
//
//
//    private String minDate(){
//        //15号是2月前，15号后是1月前
//        return DateUtil.format(DateUtil.minusMonths(DateUtil.parse(DateUtil.format(DateUtil.now(),DateUtil.PATTERN_MONTH),DateUtil.PATTERN_MONTH),2),DateUtil.PATTERN_MONTH);
//    }
//}
